package com.movecar.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import BLLocationCallbacker.DialogItemSelectCallbacker;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.movecar.R;
import com.movecar.dialog.MoveCarDialogBuilder;
import com.movecar.model.CloudBeansBean;
import com.movecar.model.GroupBean;
import com.movecar.utils.Alert;
import com.movecar.utils.AppConfig;
import com.movecar.utils.DataStore;
import com.movecar.utils.HttpPostRequestAsyncTask;
import com.movecar.utils.PopupWindowAnimation;
import com.movecar.utils.RequestResultCallback;
import com.movecar.widget.CustomProgressDialog;

/**
 * 云豆提现 手机充值
 * 
 * @author Zhao.JQ
 * 
 */

public class CloudCoinsRechargeActivity extends Activity implements android.view.View.OnClickListener,
		DialogItemSelectCallbacker {
	private EditText phoneNumber, phonenum_again, name;
	private InputMethodManager imm;
	private Button ok, back;
	private CustomProgressDialog progressDialog = null;
	private LinearLayout cash_select_linear;
	private TextView cash_select;
	private AlertDialog mDialogChoosePicture;
	private CloudBeansBean mCloudBeansBean;
	private View mainView;
	private LayoutInflater layoutInflater;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);mainView = layoutInflater.inflate(R.layout.recharge, null);
		setContentView(mainView);
		imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		initView();
	}

	private void initView() {

		back = (Button) findViewById(R.id.btn_left);
		back.setVisibility(View.VISIBLE);
		back.setOnClickListener(this);
		phoneNumber = (EditText) findViewById(R.id.phoneNumber);
		phonenum_again = (EditText) findViewById(R.id.phonenum_again);
		name = (EditText) findViewById(R.id.name);
		cash_select_linear = (LinearLayout) findViewById(R.id.cash_select_linear);
		cash_select_linear.setOnClickListener(this);
		cash_select = (TextView) findViewById(R.id.cash_select);
		ok = (Button) findViewById(R.id.ok);
		ok.setOnClickListener(this);
		TextView title = (TextView) findViewById(R.id.title);
		title.setText("话费充值");
		imm.showSoftInput(phoneNumber, InputMethodManager.RESULT_SHOWN);
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_left:
			imm.hideSoftInputFromWindow(phoneNumber.getWindowToken(), 0);
			finish();
			break;
		case R.id.ok:

			if (!"".equals(phoneNumber.getText().toString().trim())) {
				if (phoneNumber.getText().toString().trim().length() == 11) {

					if (!"".equals(phonenum_again.getText().toString().trim())) {
						if (phoneNumber.getText().toString().trim().equals(phonenum_again.getText().toString().trim())) {

							if (!"".equals(name.getText().toString().trim())) {

								if (!"".equals(cash_select.getText().toString().trim())) {

									startProgressDialog();
									phoneFare();
								} else {
									Alert.error(this, "请输入充值金额");
								}
							} else {
								Alert.error(this, "请输入姓名");
							}

						} else {
							Alert.error(this, "两次输入手机号不一致");
						}
					} else {
						Alert.error(this, "请再次输入手机号");
					}

				} else {
					Alert.error(this, "请输入正确手机号");
				}
			} else {
				Alert.error(this, "请输入手机号");
			}

			break;
		case R.id.cash_select_linear:
			(mDialogChoosePicture = MoveCarDialogBuilder.buidlListDialog(this, "", new String[] { "30元", "50元", "100元",
					"200元", "300元" }, this)).show();
			break;
		default:
			break;
		}

	}

	private void startProgressDialog() {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(this);
			progressDialog.setMessage("正在加载...");
		}

		progressDialog.show();
	}

	private void stopProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	/**
	 * 充话费
	 * 
	 */
	@SuppressWarnings("unchecked")
	private void phoneFare() {

		List<NameValuePair> params = new ArrayList<NameValuePair>(); // 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "phoneFareNew1")); // 添加要传递的参数
		params.add(new BasicNameValuePair("userId", DataStore.getInstance().getUserId()));
		params.add(new BasicNameValuePair("phone", phoneNumber.getText().toString()));
		params.add(new BasicNameValuePair("name", name.getText().toString()));
		params.add(new BasicNameValuePair("count", cash_select.getText().toString().replace("元", "")));
		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {

				String result = (String) object;
				Gson gson = new Gson();
				try {
					mCloudBeansBean = gson.fromJson(result, CloudBeansBean.class);
				} catch (Exception e) {
					return;
				}
				if ("0".equals(mCloudBeansBean.getResultCode())) {
					if (!TextUtils.isEmpty(mCloudBeansBean.getCoinAlert())) {
						new PopupWindowAnimation(mainView,CloudCoinsRechargeActivity.this);
					}
					stopProgressDialog();
					Alert.info(CloudCoinsRechargeActivity.this, "提交成功，将于2个工作日内为您充值，请留意充值情况。");
					finish();

				} else {
					stopProgressDialog();
					Alert.error(CloudCoinsRechargeActivity.this, mCloudBeansBean.getResultCode());
				}
			}

			@Override
			public void onFail(String exception) {
				stopProgressDialog();
				Alert.error(CloudCoinsRechargeActivity.this, "与服务器连接失败，请检查网络");

			}
		}, GroupBean.class, AppConfig.USER).execute(params);
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {

		if (dialog == mDialogChoosePicture) {
			if (which == 0) {
				cash_select.setText("30元");
			} else if (which == 1) {
				cash_select.setText("50元");
			} else if (which == 2) {
				cash_select.setText("100元");
			} else if (which == 3) {
				cash_select.setText("200元");
			} else if (which == 4) {
				cash_select.setText("300元");
			}
		}

	}
}
